﻿using API_Service;
using API_Service.BizLogic.BasicData;
using API_Service.BizLogic.WorkOrder;
using Nancy;
using System;
using System.Collections.Generic;
using System.Web;

namespace API_Service.Module
{
    public class WOModule : BaseAPIModule
    {
        private WOService woService = new WOService();

        public WOModule(): base("/mo")
        {
            Get["/"] = _ => Response.AsJson("产能资料维护");

            #region 产能资料维护

            //mo清单
            Get["/list"] = p =>
            {
                return Response.AsJson(woService.GetMOList());
            };

            Get["/list/{mo}"] = p =>
            {
                return Response.AsJson(woService.GetMOByID((string)p.mo));
            };
            Get["/list/{mo}/delete"] = p =>
            {
                return Response.AsJson(woService.DeleteMO((string)p.mo));
            };
            //mosn清单
            Get["/list/snlist/{mo}"] = p =>
            {
                return Response.AsJson(woService.GetMOSNList((string)p.mo));
            };

            //createnewmo
            Get["/newmo"] = p =>
            {
                ExecutionResult<dynamic> result = woService.GetNewMONumber();

                string type = string.Empty;
                string newmo = string.Empty;

                for (int i = 0; i < result.Datas.Length; i++)
                {
                    if (result.Datas[i].type == "TYPE1")
                    {
                        type = result.Datas[i].type;
                        newmo = result.Datas[i].newmo;

                        if (newmo == null)
                        {
                            continue;
                        }

                        break;
                    }
                    if (result.Datas[i].type == "TYPE2")
                    {
                        type = result.Datas[i].type;
                        newmo = result.Datas[i].newmo;
                        break;
                    }
                }

                result.Datas = new dynamic[] { new { type = type, newmo = newmo } };

                return Response.AsJson(result);
            };

            Get["/sn/{sn}"] = p =>
            {
                return Response.AsJson(woService.GetMOSNListBySN((string)p.sn));
            };

            //保存mo
            Post["/save"] = _ =>
            {
                string model = Request.Form["MODEL"];

                return Response.AsJson(woService.SaveMOData(model));
            };

            Post["/expandsn"] = _ =>
            {
                string model = Request.Form["MODEL"];

                return Response.AsJson(woService.ExpandOrderSN(model));
            };

            Post["/sn/printsave"] = _ =>
            {
                string model = Request.Form["MODEL"];

                return Response.AsJson(woService.SNPrintDataSave(model));
            };

            #endregion

        }

    }
}